﻿using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

namespace YJKRevitBase.ExcelUtility.ModelLayer
{
	public class Worksheet
	{
		internal Worksheet(object thisObject)
		{
			this.m_workSheet = thisObject;
		}

		public string Name
		{
			get
			{
				return (string)ExcelUtilityMethod.GetProperty(this.m_workSheet, "Name");
			}
			set
			{
				ExcelUtilityMethod.SetProperty(this.m_workSheet, "Name", new object[]
				{
					value
				});
			}
		}

		public Workbook Parent
		{
			get
			{
				return new Workbook(ExcelUtilityMethod.GetProperty(this.m_workSheet, "Parent"));
			}
		}

		public Range Range(string input)
		{
			return new Range(ExcelUtilityMethod.GetProperty(this.m_workSheet, "Range", new object[]
			{
				input,
				Missing.Value
			}));
		}

		public Range Range(int row, int column)
		{
			string text = ExcelUtilityMethod.ConvertToTitle(column + 1) + (row + 1).ToString();
			return new Range(ExcelUtilityMethod.GetProperty(this.m_workSheet, "Range", new object[]
			{
				text
			}));
		}

		public Range Range(int rowOne, int columnOne, int rowTwo, int columnTwo)
		{
			string str = ExcelUtilityMethod.ConvertToTitle(columnOne + 1) + (rowOne + 1).ToString();
			string str2 = ExcelUtilityMethod.ConvertToTitle(columnTwo + 1) + (rowTwo + 1).ToString();
			string text = str + ":" + str2;
			return new Range(ExcelUtilityMethod.GetProperty(this.m_workSheet, "Range", new object[]
			{
				text
			}));
		}

		public Range get_Range(string input)
		{
			return this.Range(input);
		}

		public void Cell(int row, int column, dynamic input)
		{
			string text = ExcelUtilityMethod.ConvertToTitle(column + 1) + (row + 1).ToString();
			ExcelUtilityMethod.SetProperty(ExcelUtilityMethod.GetProperty(this.m_workSheet, "Range", new object[]
			{
				text
			}), "Value", new object[]
			{
				input
			});
		}

        //[return: Dynamic]
        //public dynamic Cell(int row, int column)
        //{
        //    string text = ExcelUtilityMethod.ConvertToTitle(column + 1) + (row + 1).ToString();
        //    return ExcelUtilityMethod.GetProperty(ExcelUtilityMethod.GetProperty(this.m_workSheet, "Range", new object[]
        //    {
        //        text
        //    }), "Value");
        //}

		public void Select()
		{
			ExcelUtilityMethod.UseMethod(this.m_workSheet, "Select", null);
		}

		public YJKRevitBase.ExcelUtility.ModelLayer.Shape AddPicture(string strTempPath, float left, float top, float tempImageHeight, float tempImageWidth)
		{
			return new YJKRevitBase.ExcelUtility.ModelLayer.Shape(ExcelUtilityMethod.UseMethod(ExcelUtilityMethod.GetProperty(this.m_workSheet, "Shapes"), "AddPicture", new object[]
			{
				strTempPath,
				MsoTriState.msoFalse,
				MsoTriState.msoTrue,
				left,
				top,
				tempImageWidth,
				tempImageHeight
			}));
		}

		public Chart AddChart(double left = 0.0, double top = 0.0)
		{
			Microsoft.Office.Interop.Excel.XlChartType xlChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnClustered;
			return new Chart(ExcelUtilityMethod.GetProperty(ExcelUtilityMethod.UseMethod(ExcelUtilityMethod.GetProperty(this.m_workSheet, "Shapes"), "AddChart", new object[]
			{
				xlChartType,
				left,
				top
			}), "Chart"));
		}

		private object m_workSheet;
	}
}
